package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteByRoleRelation(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        //给员工添加角色
        if (ids != null && ids.length>-1){
            for (Long id : ids){
                employeeMapper.insertByRoleRelation(employee.getId(),id);
            }
        }
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //先删除员工与角色关联
        employeeMapper.deleteByRoleRelation(employee.getId());
        if (ids != null && ids.length>-1){
            //再给员工添加角色
            for (Long id : ids){
                employeeMapper.insertByRoleRelation(employee.getId(),id);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.queryForDate(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Employee selectByName(String name,String password) {
        Employee employee = employeeMapper.selectByName(name);
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            throw  new BusinessException("用户名或密码不能为空!");
        }
        if (employee==null){
            throw new BusinessException("用户名不存在!");
        }
        if (!employee.getPassword().equals(password)){
            throw new BusinessException("密码错误!");
        }
        return employee;
    }

    @Override
    public List<Permission> selectAllPermission(Long id) {
        return employeeMapper.selectAllPermission(id);
    }

    @Override
    public Workbook exportXls() {
        //创建文本对象
        Workbook wb = new HSSFWorkbook();
        //创建表单
        Sheet sheet = wb.createSheet("员工列表");
        //设置标头
        Row rowHead = sheet.createRow(0);
        //设置第一列
        rowHead.createCell(0).setCellValue("用户名");
        //设置第二列
        rowHead.createCell(1).setCellValue("邮箱");
        //设置第三列
        rowHead.createCell(2).setCellValue("年龄");
        //查询所有员工信息
        List<Employee> employees = employeeMapper.selectAll();

        for (int i=0;i<employees.size();i++){
            //获取单个员工信息
            Employee employee = employees.get(i);
            //从第2行开始
            Row row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //把文件解析成Workbook对象，并接收
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取第一张表
        Sheet sheet = wb.getSheetAt(0);
        //获取最后一条数据索引
        int lastRowNum = sheet.getLastRowNum();
        //从第二行数据开始读取
        for (int i = 1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            //创建对象接收参数，并插入数据
            Employee emp = new Employee();
            emp.setName(row.getCell(0).getStringCellValue());
            emp.setEmail(row.getCell(1).getStringCellValue());
            Double age = row.getCell(2).getNumericCellValue();
            emp.setAge(age.intValue());
            emp.setPassword("123");
            //插入数据
            employeeMapper.insert(emp);
        }
    }
}
